<?php


namespace App\Utility;


class FinanceUtil
{
    /**
     * 今天开始
     * @return false|int
     */
    public static function todaystart()
    {
        return strtotime(date("Y/m/d 00:00:00"));
    }

    /**
     * 今天结束
     * @return false|int
     */
    public static function todayend()
    {
        return strtotime(date("Y/m/d 23:59:59"));
    }

    /**
     * 9点
     */
    public static function today9hour()
    {
        return strtotime(date("Y/m/d 12:59:59"));
        //return strtotime(date("Y/m/d 14:29:59"));
    }

    /**
     * 15点
     * @return false|int
     */

    public static function today15hour()
    {
        return strtotime(date("Y/m/d 15:15:00"));
    }


    /**
     * 判断开始时间
     * @return false|int
     */
    public static function now2h15()
    {
        //如果是星期六
        $week = date("w");
        if ($week == 6) {
            return self::today15hour() - 86400;
        }
        if ($week == 0) {
            return self::today15hour() - 172800;
        }
        $now = time();

        if ($now > self::today15hour()) {
            return self::today15hour();
        } else {
            if ($week == 1) {
                return self::today15hour() - 259200;
            }
            return self::today15hour() - 86400;
        }
    }

    /**
     * 用于统计
     * 判断开始时间
     * @return false|int
     */
    public static function now2h15guess()
    {
        //如果是星期六
        $week = date("w");
        if ($week == 6) {
            return self::today15hour() - 86400;
        }
        if ($week == 0) {
            return self::today15hour() - 172800;
        }

        if ($week == 1) {
            return self::today15hour() - 259200;
        }

        return self::today15hour() - 86400;

    }

    /**
     * 到15点的时间差
     * @return false|int
     */
    public static function now2h15time()
    {
        $now = time();
        $week = date("w");
        if ($week == 6) {
            return self::today15hour() + 172800 - $now;
        }
        if ($week == 0) {
            return self::today15hour() + 86400 - $now;
        }
        if ($now > self::today15hour()) {
            return self::today15hour() + 86400 - $now;
        } else {
            return self::today15hour() - $now;
        }

    }

    /**
     * 到今天结束的时间差
     * @return false|int
     */
    public static function now2todayend()
    {
        return self::todayend() - time();
    }

    /**
     * 开市和休市
     */
    public static function is_finance()
    {


        $week = date("w");
        //周六日休市
        if ($week == 0 || $week == 6) {
            return false;
        }
        if (self::is_spdate()) {
            //特殊日子
            return false;
        }

        $now = time();
        if ($now >= strtotime(date("Y/m/d 09:00:00")) && $now <= self::today15hour()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否开盘时间
     */
    public static function is_guess_time()
    {

        //如果是 周六 周日
        $week = date("w");

        if ($week == 0 || $week == 6) {
            return true;
        }

        if (self::is_spdate()) {
            //特殊日子
            return true;
        }

        $now = time();

        if ($now >= self::today9hour() && $now <= self::today15hour()) {
            //不在竞猜时间
            return false;
        } else {
            return true;
        }


    }


    public static function nextTradeDayTime()
    {
        return strtotime(self::nextTradeDay());
    }

    public static function lastTradeDayTime()
    {
        return strtotime(self::lastTradeDay());
    }

    public static function is_spdate()
    {

        $falg = false;
        /**
         * $d = date("Ymd");
         * switch ($d){
         * case '20190913':
         * case '20191001':
         * case '20191002':
         * case '20191003':
         * case '20191004':
         * $falg = true;
         * break;
         * }*/
        return $falg;
    }


    //计算下个交易日期
    public static function nextTradeDay()
    {
        $date_str = date("Ymd");
        /**
         * //是否特殊日子
         * if($date_str == '20190912'){
         * $now  = time();
         * if($now  <= self::today15hour()){
         * return $date_str;
         * }else{
         * return '20190916';
         * }
         * }
         * if($date_str == '20190913'){
         * return '20190916';
         * }
         *
         * if($date_str == '20190930'){
         * $now  = time();
         * if($now  <= self::today15hour()){
         * return $date_str;
         * }else{
         * return '20191008';
         * }
         * }
         * if($date_str == '20191001' ||$date_str == '20191002'||$date_str == '20191003'||$date_str == '20191004'){
         * return '20191008';
         * }
         */

        $week = date("w");
        if ($week == 6) {
            return date("Ymd", strtotime("+2 day"));
        }
        if ($week == 0) {
            return date("Ymd", strtotime("+1 day"));
        }
        $now = time();
        if ($now <= self::today15hour()) {
            return $date_str;
        } else {
            if ($week == 5) {
                return date("Ymd", strtotime("+3 day"));
            }
            return date("Ymd", strtotime("+1 day"));
        }

    }

    //计算上个交易日期
    public static function lastTradeDay()
    {
        $date_str = date("Ymd");

        /**
         * $a_arr = ['20191001','20191002','20191003','20191004','20191005','20191006','20191007'];
         * if(in_array($date_str,$a_arr)){
         * return '20190930';
         * }
         * if($date_str == '20191008'){
         * $now  = time();
         * if($now  > self::today15hour()){
         * return $date_str;
         * }else{
         * return '20190930';
         * }
         * }
         */
        $week = date("w");
        if ($week == 6) {
            return date("Ymd", strtotime("-1 day"));
        }
        if ($week == 0) {
            return date("Ymd", strtotime("-2 day"));
        }
        $now = time();
        if ($now > self::today15hour()) {
            return $date_str;
        } else {
            if ($week == 1) {
                return date("Ymd", strtotime("-3 day"));
            }
            return date("Ymd", strtotime("-1 day"));
        }
    }


    public static function isHoliday()
    {
        $week = date("w");
        if ($week == 0 || $week == 6) {
            return true;
        }
        /**
         * $date_str = date("Ymd");
         * $a_arr = ['20191001','20191002','20191003','20191004','20191005','20191006','20191007'];
         * if(in_array($date_str,$a_arr)){
         * return true;
         * }*/
        return false;

    }

}